<?php

namespace Console\Controller;

use Common\Controller\BaseController;

class ImageController extends BaseController {

    /**
     * 图片上传
     */
    public function doUpload() {
        $fileData = $_FILES['file'];
        if ($fileData['error'] != 0) {
            $this->ajaxReturn(static::$res->err("图片上传失败"));
        }
        $fileInfo = getimagesize($fileData['tmp_name']);
        $mime = $fileInfo['mime'];
        if (preg_match('/image\/\w+/', $mime) != 1) {
            $this->ajaxReturn(static::$res->err("仅支持图像上传"));
        }

        $oldImgName = $fileData['name'];
        if (strrpos($oldImgName, '.')) {
            $imgType = substr($oldImgName, strrpos($oldImgName, '.') + 1);
        } else {
            $imgType = 'jpeg';
        }

        $imgBase64 = base64_encode(file_get_contents($fileData['tmp_name']));
        $fileName = "/ImgFile/" . date("Ymd") . "/" . time() . "_" . rand(1111, 9999) . '.' . $imgType;

        if (!saveImg($imgBase64, WEB_PATH . $fileName)) {
            $this->ajaxReturn(static::$res->err("图像保存失败"));
        }

        $res = array(
            "url" => "/pigeonSys" . $fileName
        );
        $this->ajaxReturn(static::$res->suc("成功", $res));
    }

}
